import React, { useEffect, useState } from 'react'
import { Form, Button, Popconfirm, Modal, DatePicker, Row, Col,Input } from 'antd'
import { connect } from 'umi'
import moment from 'moment'

const FormItem = Form.Item
const { TextArea } = Input

const formLayout = {
  labelCol: { span: 7 },
}

const CreateForm = (props) => {
  const { onSubmit, onCancel,object, visible } = props
  const [form] = Form.useForm()

  useEffect(() => {
   if(object){
     form.setFieldsValue({regularDay:moment(object.regularDay),regularRemark:object.regularRemark})
   }
  }, visible)

  const onFormCancel = () => {
    form.resetFields()
    onCancel()
  }

  const handleSubmit = async () => {
    const fieldsValue = await form.validateFields()
    form.resetFields()
    onSubmit({
      ...fieldsValue,
      oid:object.oid,
      isRegular:object.isRegular
    })
  }

  const renderContent = () => {
    return (
      <>
        <Row gutter={24}>
          <Col span={20}>
            <FormItem name="regularDay" label="转正日期" rules={[{ required: true, message: '请选择转正日期！' }]}>
              <DatePicker placeholder="请选择日期" style={{ width: '100%' }}/>
            </FormItem>
          </Col>
          <Col span={20}>
            <FormItem name="regularRemark" label="延期/提前转正说明" rules={[{ required: true, message: '请输入延期/提前转正原因！' }]}>
              <TextArea rows={4} />
            </FormItem>
          </Col>
        </Row>
      </>
    )
  }

  const renderFooter = () => {
    return (
      <>
        <Button onClick={() => onFormCancel()}>取消</Button>
        <Popconfirm title={`是否保存修改后的转正日期?`}
                    onConfirm={handleSubmit}
                    okText="是"
                    cancelText="否">
          <Button type="primary">提交</Button>
        </Popconfirm>
      </>
    )
  }

  return (
    <Modal
      width={1024}
      destroyOnClose
      title="更新转正日期"
      visible={visible}
      footer={renderFooter()}
      onOk={handleSubmit}
      onCancel={() => onFormCancel()}
      onClose={() => onFormCancel()}
      okText="确认"
      cancelText="取消"
    >
      <Form
        {...formLayout}
        form={form}
        style={{ paddingTop: '24px' }}
      >
        {renderContent()}
      </Form>
    </Modal>
  )
}


export default connect(() => ({
}))(CreateForm)
